﻿using FourGroups.Project;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using WMS.Seven.Models;

namespace WMSProject.SuJiaHao
{
    /// <summary>
    /// 库位管理服务接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "苏佳豪")]
    public class WarehouseLocationService : ApplicationService,IWarehouseLocationService
    {
        /// <summary>
        /// 库位管理仓储
        /// </summary>
        private readonly IRepository<WarehouseLocationManagement, int> _wareHouseRepository;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="wareHouseRepository">库位管理仓储</param>
        public WarehouseLocationService(IRepository<WarehouseLocationManagement, int> wareHouseRepository)
        {
            _wareHouseRepository = wareHouseRepository;
        }

        /// <summary>
        /// 获取库位管理列表
        /// </summary>
        /// <param name="pageSize">页容量</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns>返回销售退货的对象把</returns>
        public async Task<ReturnSelect<object>> GetWarehouseLocationList(string locationNumber, string locationName, int pageSize, int pageIndex)
        {
            // 获取库位管理列表
            var wareHouseList = await _wareHouseRepository.GetListAsync();

            // 使用 WhereIf 进行条件筛选
            var wareHouseLists = wareHouseList
                .WhereIf(!string.IsNullOrEmpty(locationNumber), x => x.LocationNumber == locationNumber)
                .WhereIf(!string.IsNullOrEmpty(locationName), x => x.LocationName.Contains(locationName))
                .ToList();

            // 计算总记录数和当前页记录
            var totalCount = wareHouseLists.Count;
            var pageList = wareHouseLists.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            var result = AutoMapperExt.MapToList<WarehouseLocationManagement>(pageList);

            //返回结果
            var data = new { TotalCount = totalCount, PageList = result };
            return new ReturnSelect<object>(true, "0", result != null && result.Count > 0 ? data : null);
        }

        /// <summary>
        /// 库位管理添加
        /// </summary>
        /// <param name="input">库位管理实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> GetCreateWarehouseLocation(CreateWarehouseLocationDTO input)
        {
            var warehouseLocation = ObjectMapper.Map<CreateWarehouseLocationDTO, WarehouseLocationManagement>(input);
            warehouseLocation.Remark = "111";
            warehouseLocation.CreateDate=DateTime.Now;
            //sasles.CreateBy = "崔二强";
            //sasles.CreateTime = DateTime.Now;
            await _wareHouseRepository.InsertAsync(warehouseLocation);
            return warehouseLocation != null ? 1 : 0;
        }

        /// <summary>
        /// 库位管理信息删除
        /// </summary>
        /// <param name = "id" > 条件编号 </ param >
        /// < returns > 返回受影响行数 </ returns >
        public async Task<int> DeleteWarehouseLocation(int id)
        {
            var warehouseLocationDel = await _wareHouseRepository.GetAsync(id);
            await _wareHouseRepository.DeleteAsync(warehouseLocationDel);
            return warehouseLocationDel != null ? 1 : 0;
        }

        /// <summary>
        ///库位管理反填
        /// </summary>
        /// <param name="id">条件编号</param>
        /// <returns>返回库位管理实体</returns>
        public async Task<WarehouseLocationManagement> GetWarehouseLocationManagement(int id)
        {
            var warehouse = await _wareHouseRepository.FirstOrDefaultAsync(x => x.Id == id);
            return warehouse;
        }

        /// <summary>
        /// 销售退货信息修改
        /// </summary>
        /// <param name="input">仓库实体</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdateWarehouseLocationManagement(CreateWarehouseLocationDTO input)
        {
            var warehouseLocationManagement = ObjectMapper.Map<CreateWarehouseLocationDTO, WarehouseLocationManagement>(input);
            warehouseLocationManagement.Remark = "111";
            warehouseLocationManagement.CreateDate = DateTime.Now;
            await _wareHouseRepository.UpdateAsync(warehouseLocationManagement);
            return warehouseLocationManagement != null ? 1 : 0;
        }

    }
}
